// pages/store/classify/index.js
import {
  getStoreByClassifyIdApi
} from '../../../https/store'
import {
  BASE_URL
} from '../../../constant/index'
Page({

  data: {
    BASE_URL,
    list: [],
    classify: {},
    total: 0,
    sort: ['排序', '降序', '升序'],
    sortIndex: 0,
    tabbar: ["全部", "新品"],
    tabbarIndex: 0
  },

  onLoad(options) {
    console.log(options)
    const docid = options.docid

    this.getInitData(docid)
    this.setData({
      docid
    })
  },
  async getInitData(docid) {
    const tabbar = this.data.tabbar
    const tabbarIndex = this.data.tabbarIndex
    const getRes = await getStoreByClassifyIdApi({
      id: docid,
      type: tabbar[tabbarIndex]
    })
    console.log(getRes)
    if (getRes.code == 200) {
      // 进行排序
      const sortIndex = this.data.sortIndex
      if (sortIndex == 1) {
        getRes.data.list.sort((a, b) => b.price - a.price)
      }
      if (sortIndex == 2) {
        getRes.data.list.sort((a, b) => a.price - b.price)
      }

      this.setData({
        list: getRes.data.list,
        total: getRes.data.total,
        classify: getRes.data.classify,
      })
    }
  },
  bindPickerChange(e) {
    console.log(e)
    const index = e.detail.value
    this.setData({
      sortIndex: index
    })
    const docid = this.data.docid
    this.getInitData(docid)
  },
  switchTabber(e) {
    console.log(e)
    const index = e.currentTarget.dataset.index
    const docid = this.data.docid
    this.setData({
      tabbarIndex: index
    })
    this.getInitData(docid)
  },
  // 跳转详情页
  gotoDetail(e) {
    console.log(e)
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../detail/index?docid=' + docid,
    })
  }
})